package org.pine.dictionary.service.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.pine.dictionary.common.StatusEnum;
import org.pine.dictionary.dao.entity.Dictionary;
import org.pine.dictionary.dao.mapper.DictionaryMapper;
import org.pine.dictionary.service.bo.DictionaryBo;
import org.pine.dictionary.service.query.DictionaryQuery;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

/**
 * <p>
 * 字典表管理类
 * </p>
 *
 * @author pine
 * @since 2024-05-16
 */
@Slf4j
@Service
public class DictionaryManager extends BaseManager<Dictionary, DictionaryBo, DictionaryMapper, DictionaryQuery, Long, String> {


    /**
     * 查询单条数据
     *
     * @param dictionaryId 字典ID
     * @return 字典业务对象
     */
    public DictionaryBo getOne(String dictionaryId) {
        return getOne(new DictionaryQuery().setDictionaryId(dictionaryId));
    }

    /**
     * 查询单条数据
     *
     * @param request 查询参数
     * @return 字典业务对象
     */
    public DictionaryBo getOne(DictionaryQuery request) {
        // 处理业务查询条件
        LambdaQueryWrapper<Dictionary> queryWrapper = this.encapsulateQueryWrapper(request);
        queryWrapper.last(" limit 1");

        // 查询单条数据
        Dictionary dictionary = this.mapper.selectOne(queryWrapper);
        if (Objects.isNull(dictionary)) {
            return null;
        }

        // 根据查询结果转换业务对象
        return createBo(Collections.singletonList(dictionary)).get(0);
    }


    /**
     * 封装查询条件
     *
     * @param request 请求参数
     * @return 查询条件wrapper
     */
    public LambdaQueryWrapper<Dictionary> encapsulateQueryWrapper(DictionaryQuery request) {
        // 处理业务查询条件
        LambdaQueryWrapper<Dictionary> queryWrapper = super.encapsulateQueryWrapper(request);
        queryWrapper.eq(StrUtil.isNotBlank(request.getDictionaryId()), Dictionary::getDictionaryId, request.getDictionaryId());
        queryWrapper.eq(StrUtil.isNotBlank(request.getCode()), Dictionary::getCode, request.getCode());
        queryWrapper.eq(StrUtil.isNotBlank(request.getName()), Dictionary::getName, request.getName());
        queryWrapper.eq(Objects.nonNull(request.getStatus()), Dictionary::getStatus, request.getStatus());
        return queryWrapper;
    }

    /**
     * 通过业务id列表批量删除
     *
     * @param dictionaryIds id列表
     */
    public void removeBatchByIds(Collection<String> dictionaryIds) {
        super.removeBatchByIds(dictionaryIds, Dictionary::getDictionaryId);
    }

    /**
     * 修改状态
     *
     * @param dictionaryIds 业务id列表
     * @param status        状态
     */
    public void changeStatus(Collection<String> dictionaryIds, StatusEnum status) {
        if (CollUtil.isNotEmpty(dictionaryIds)) {
            LambdaUpdateWrapper<Dictionary> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.in(Dictionary::getDictionaryId, dictionaryIds);
            updateWrapper.ne(Dictionary::getStatus, status);
            updateWrapper.set(Dictionary::getStatus, status);
            updateWrapper.set(Dictionary::getUpdateTime, LocalDateTime.now());
            // TODO 更新的用户信息，待维护
            updateWrapper.set(Dictionary::getUpdateUserId, 0L);
            updateWrapper.set(Dictionary::getUpdateUsername, "system");
            this.mapper.update(updateWrapper);
        }
    }
}
